import { Injectable, Logger } from '@nestjs/common';
import { createClient, RedisClientOptions, RedisClientType } from 'redis';

@Injectable()
export class RedisService {
  private readonly client: RedisClientType;
  constructor() {
    // this.client = createClient({ url: 'redis://127.0.0.1:6379' });
    // // 监听错误信息
    // this.client.on('err', (err) => {
    //   Logger.error(err, 'RedisService');
    // });
    // this.client.connect().then(() => {});
  }

  get(key: string): Promise<string> {
    return new Promise((resolve, reject) => {
      this.client
        .get(key)
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          reject(err);
        });
    });
  }

  set(key: string, value: string): Promise<void> {
    return new Promise((resolve, reject) => {
      this.client
        .set(key, value)
        .then(() => {
          resolve();
        })
        .catch((err) => {
          reject(err);
        });
    });
  }
}
